$(function () {
	/*判断用户是否是医生*/
	var docto,userid;
	ym.ajax({
		url : "/user/getUser",
		async : false,
		success : function(data){
			if(data.res == 1){
				var html ="";
				if(data.obj.type == 1){
					userid = data.obj.userId;
					$('.question').addClass('none');
					html += '<a href="javascript:void(0);">';
					html += '<img src="../img/yimiao.png">';
					html += '<span class="am-navbar-label">问答</span>';
					html += '</a>';
					$('.doctoras').removeClass('yimiao');
					$('.doctoras').addClass('yimiao1');
				}else{
					$('.question').removeClass('none');
					html += '<a href="javascript:void(0);">';
					html += '<img src="../img/yimiao.png">';
					html += '<span class="am-navbar-label">疫苗</span>';
					html += '</a>';
					$('.doctoras').addClass('yimiao');
				}
				$('.doctoras').html(html);
				ym.ajax({
					url: "/askDoctor/queryDoctorId",
					param:{
						userId:userid
					},
					success: function (data){
					   docto = data.obj;
					}
				});
			}else{console.log("没有获取到当前用户的身份")}
			
		}
	});
	
	$('body').on('click','.yimiao1',function(){
		window.location.href="Doctor.html?doctorid="+docto+"&userid="+userid+"&a=2";
	})
	/*课程包*/
 	ym.ajax({
        url:"/course/queryCoursePackage",
        success:function (data) {
        	console.log(data.obj.results);
            var htmlli='';
            if(data.obj.results.length > 0){
            	var n=data.obj.results.length;
            	if(n>3){
                	n=3;
	            }
	            for(var i=0;i<n;i++){
	            	htmlli+='<li class="am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left" onclick="javascript:window.location.href=\'packagedetails.html?coursePackageId='+data.obj.results[i].coursePackageId+'\'">\n' 
	                   htmlli+= '<div class="am-u-sm-3 am-list-thumb">\n' 
	                    htmlli+='<a class="">\n' 
	                    htmlli+='<img src="'+data.obj.results[i].attachmentUrl+'" alt="疫苗！"/>\n' 
	                   htmlli+= '</a>\n' 
	                    htmlli+='</div>\n' 
	                    htmlli+='<div class=" am-u-sm-8 am-list-main w72">\n' 
	                    htmlli+='<h3 class="alt w100"><a class="w80 twospace" style="display: inline-block;">'+data.obj.results[i].coursePackageName+'</a>'
			                    htmlli+=	'<p class="floatr font13 colorb6cce fontw" style="margin-top:1px">节数:<i>'+data.obj.results[i].total+'</i></p>'
			                  htmlli+=  '</h3>\n'
	                    
	                   htmlli+= '<p class="learning">\n' 
	                   htmlli+= '<span>已售:'+data.obj.results[i].saleTotalNumber+'</span>\n' 
	                   htmlli+= '<!--两种情况-->\n' 
	                   htmlli+= '<span class="price">优惠价: ￥'+data.obj.results[i].discountPrice/100+'</span>\n' 
	                   htmlli+= '<!--<span class="free">免费</span>-->\n' 
	                   htmlli+= '</p>\n' 
	                   htmlli+= '</div>\n' 
	                   htmlli+= '</li>';
	             }
	            $('.lessons').append(htmlli);
            }else{
            	htmlli = '<li class="am-text-center"><img style="height:150px" src="../img/no.jpg"></li>';
				$('.lessons').append(htmlli);
            }
        }
    })
    /*轮播图*/
   	ym.ajax({
		url:"/course/queryCoursePackage",
		success:function(data){
			console.log(data);
			var $slider = $('#demo-slider-0');
			if(data.res ==1){
				var lengt = data.obj.results.length;
				if(lengt > 4){
					var num = lengt - 4;
					for(var i=num;i<data.obj.results.length;i++){
						$slider.flexslider('addSlide', '<li onclick="javascript:window.location.href=\'packagedetails.html?coursePackageId='+data.obj.results[i].coursePackageId+'\'"><img src="'+data.obj.results[i].attachmentUrl+'">');
						$slider.flexslider('removeSlide', 0);
					}
				}else{
					for(var i=0;i<data.obj.results.length;i++){
						$slider.flexslider('addSlide', '<li onclick="javascript:window.location.href=\'packagedetails.html?coursePackageId='+data.obj.results[i].coursePackageId+'\'"><img src="'+data.obj.results[i].attachmentUrl+'">');
						$slider.flexslider('removeSlide', 0);
					}
				}
				
			}
			else{console.log("数据获取失败！")}
		}
	});
	
	/*跳转到医生页面*/
    $('.question').click(function(){
            ym.ajax({
                url:"/askDoctor/queryDoctorId",
                param:{
                    userId:ym.userId
                },
                success:function(data){
                    if(data.res ==1){
                        /*用户登录*/
                        if(data.obj){
                            var userId=data.obj;
                            ym.ajax({
                                url: "/askDoctor/queryDoctorInfo",
                                param:{
                                    userId:userId
                                },
                                success: function (data) {
                                   if(data.obj){
                                        sessionStorage.setItem('curData',JSON.stringify(data.obj));
                                        window.location.href='askDoctor.html';
                                   }
                                }
                            });
                        }
                        
                    }
                    else{
                        window.location.href='zixunOnline.html?a=2';
                    }
                }
            });
    })
})